using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class DamagePopup : MonoBehaviour {
    [Header("组件引用")]
    public Text damageText;

    [Header("动画设置")]
    public float floatDistance = 50f;      // Y 轴漂浮距离（像素）
    public float duration = 1f;            // 总时长
    public Vector2 randomXRange = new Vector2(-30f, 30f); // X 轴随机偏移范围

    /// <summary>
    /// 初始化显示
    /// </summary>
    public void Init(int damage, DamageType type) {
        // 文字／颜色
        if (damage <= 0) {
            damageText.text = "Miss";
            damageText.color = Color.gray;
        }
        else {
            damageText.text = damage.ToString();
            switch (type) {
                case DamageType.Physical:
                    damageText.color = Color.red;    break;
                case DamageType.True:
                    damageText.color = Color.yellow; break;
                case DamageType.Magic:
                    damageText.color = new Color(0.3f, 0.8f, 1f); // 浅蓝
                    break;
            }
        }
        // print(transform.parent.name);
        // 播放动画
        PlayTween();
    }

    private void PlayTween() {
        RectTransform rt = GetComponent<RectTransform>();
        Vector2 startPos = rt.anchoredPosition;
        Vector2 endPos = startPos + new Vector2(
            Random.Range(randomXRange.x, randomXRange.y),
            floatDistance
        );

        // Sequence 串联移动和渐隐
        Sequence seq = DOTween.Sequence();
        seq.Append(rt.DOAnchorPos(endPos, duration).SetEase(Ease.OutCubic));
        seq.Join(damageText.DOFade(0f, duration).SetEase(Ease.InQuad));
        seq.OnComplete(() => Destroy(gameObject));
    }
}
public enum DamageType {
    Physical,
    True,
    Magic
}